package com.xdap.keymen.service.dayang;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.xdap.keymen.entity.dayang.DaYangMaterialEntity;
import com.xdap.keymen.entity.dayang.DaYangShopMapEntity;
import com.xdap.keymen.entity.dayang.DaYangStoreOrderEntity;
import com.xdap.keymen.entity.dayang.DayangResponse;
import com.xdap.keymen.utils.OKHttpUtils;
import com.xdap.motor.vo.ResponseVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class DaYangMaterialService extends BaseService{
    @Autowired
    OKHttpUtils okHttpUtils;

    @Autowired
    DaYangShopMapService dayangShopMapService;
    /*
     * 获取物料信息
     */
    public ResponseVo getMaterial(String shopDefan, String ftcode) {
        DaYangShopMapEntity dayangShopMapEntity = new DaYangShopMapEntity();
        dayangShopMapEntity.setShopDayang(shopDefan);
        DaYangShopMapEntity shop=dayangShopMapService.getShopMap(dayangShopMapEntity);
        if (shop == null) {
            return ResponseVo.error("店铺不存在");
        }
        Map<String, String> params=new HashMap<>();
        params.put("shopid", shop.getShopDingzhi());
        params.put("ftcode",ftcode);
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "Bearer "+getToken());
        Gson gson = new GsonBuilder().serializeNulls().create();
        String response = okHttpUtils.doGet(getBaseUrl().concat("/api/materials"), params,headers);
        DayangResponse<DaYangMaterialEntity> dyResponse = gson.fromJson(response, new TypeToken<DayangResponse<DaYangMaterialEntity>>(){}.getType());

        DaYangMaterialEntity entity=dyResponse.getData().isEmpty()?null : dyResponse.getData().get(0);
        return  ResponseVo.ok().setData(entity);
    }
}
